﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;

namespace PiplineSample
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRouting();
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(options =>
                {
                    options.LoginPath = "/Account/Login";
                    options.LogoutPath = "/Account/Logout";
                });
            services.AddMvc();
            //services.AddAuthorization();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseAuthentication();
            app.UseRouter(routes =>
            {
                //routes.DefaultHandler = new RouteHandler(async context =>
                //{
                //    await context.Response.WriteAsync("<p>......</p>");
                //});
                routes.MapGet("Default", async context =>
                {
                    await context.Response.WriteAsync("<p>Default route page.</p>");
                });
            });
            app.UseMvcWithDefaultRoute();
            app.Map("/Task", taskApp =>
            {
                taskApp.Run(async context =>
                {
                    await context.Response.WriteAsync("Hello,this is task page.");
                });
            });
            app.Use(async (context, next) =>
            {
                await context.Response.WriteAsync("Hello,this is aspnet core's one usage of piplines");
                await next();
            });
            app.Use(handler =>
            {
                return async context =>
                {
                    await context.Response.WriteAsync("Hello,this is aspnet core's other usage of piplines");
                    await handler(context);
                };
            });
            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }
    }
}

